package com.icedata.sika.core

import com.icedata.sika.core.CommandProcessor.Result
import com.icedata.sika.util.readAsync
import okhttp3.internal.notifyAll

/**
 * 指令处理链
 */
class CommandProcessorBus(sika: Sika) : AsyncProcessorBus<CommandProcessor, Command, Result>(sika) {


    /**
     * 广播一条指令
     */
    override suspend fun broadcast(data: Command) {
        lock.readAsync(suspend {
            for (comm in processors) {
                if (!comm.filter(data))
                    continue

                when (comm.execute(data, sika)) {
                    Result.ADOPT -> {
                    }
                    Result.BLOCK -> break
                }
                //TODO("移除处理器的实现")
            }
            synchronized(lock) {
                lock.notifyAll()
            }
        })
    }
}